#include "btree.hpp"

class BTreeEx : public BTree
{
public:
    void BuildFromPreAndIn(char const* preStr, char const* inStr)
    {
        root = BuildFromPreAndIn(preStr, inStr, strlen(inStr));
    }
private:
    BTreeNode* BuildFromPreAndIn(char const* preStr, char const* inStr, int inLen)
    {
        char chRoot = *preStr;
        BTreeNode* pNode = new BTreeNode;
        pNode->value = chRoot;
        
        char* pPos = strchr(inStr, chRoot);
        int dist = static_cast<int>(pPos - inStr);
        
        if (0 != dist)
        {
            pNode->pLeft = BuildFromPreAndIn(preStr + 1, inStr, dist);
        }
        if (inLen - 1 != dist)
        {
            pNode->pRight = BuildFromPreAndIn(preStr + dist + 1, pPos + 1, inLen - dist - 1);
        }
        
        return pNode;
    }
};

int main(void)
{
    BTreeEx testTree;
	testTree.BuildFromPreAndIn("ABCDEFGHK", "BDCAEHGKF");
	char str[50] = {0};
	testTree.ConvertTo(str);
	cout<< str <<endl;
	
    system("pause");
}
